package ddemo.viewAndcontroller;

import ddemo.pojo.Charger;
import ddemo.pojo.Interface;
import ddemo.pojo.User;
import ddemo.service.ChargerService;
import ddemo.service.InterfaceService;

import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class chongdianView {
    public static void Chongdian(User user){
        List<Charger> characters;
        characters= ChargerService.searchChargers();
        System.out.println(characters.size());
        Set<Integer> chargerids=new HashSet<>();
        while(true) {
            System.out.println("可用的充电站有:");
            for (Charger c : characters) {
                System.out.println("充电站id:" + c.getId() + " " + "充电站接口数:" + c.getChargerNumber() + " " + "充电站位置:" + c.getPosition());
                chargerids.add(c.getId());
            }
            System.out.println("--------------------------------");
            System.out.println("请选择你要使用的充电站id:");
            int num;
            while (true) {
                try {
                    num = Integer.parseInt(new Scanner(System.in).next());
                } catch (NumberFormatException e) {
                    System.out.println("请输入正确的数字");
                    continue;
                }
                if (!chargerids.contains(num)) {
                    System.out.println("不存在对应的充电站,请重新输入");
                    continue;
                }
                break;
            }
            Set<Integer> interfaceid = new HashSet<>();
            List<Interface> interfaces = InterfaceService.searchIntegers(num);
            for (Interface inter : interfaces) {
                if ("可用".equals(inter.getUsestatus())) {
                    interfaceid.add(inter.getId());
                }
                System.out.println("充电口id：" + inter.getId() + " " + "充电口使用状态:" + inter.getUsestatus() + " " + "对应充电站id:" + inter.getChargerid());
            }
            System.out.println("--------------------------------");

            System.out.println("请选择你要使用的充电站id:");
            while (true) {
                try {
                    num = Integer.parseInt(new Scanner(System.in).next());
                } catch (NumberFormatException e) {
                    System.out.println("请输入正确的数字");
                    continue;
                }
                break;
            }
            if (!interfaceid.contains(num)) {
                System.out.println("不存在对应的充电站或者充电站已被使用,请重新输入");
                continue;
            }

            for (Interface inter : interfaces) {
                if (inter.getId() == num) {
                    inter.setUsestatus("已使用");
                    InterfaceService.UpdateStatus("已使用", inter.getId(),user.getId());
                }
            }
            System.out.println("选择完成,您已可以开始使用充电桩");
            System.out.println("--------------------------------");
            break;
        }
    }
}

